package com.itheima.utils;

import java.util.Calendar;

public class GetAgeUtil {


    public static String evaluate(String idCardNo) {
        if (idCardNo == null || "".equals(idCardNo)) {
            return "身份证件号有误,无法计算年龄";
        }

        if (idCardNo.length() != 15 && idCardNo.length() != 18) {
            return "身份证件号有误,无法计算年龄";
        }

        //TODO 此处未添加身份证号校验，若需要，可自行添加

        String age = "身份证件号有误,无法计算年龄";

        if (idCardNo != "身份证件号有误") {
            Calendar cal = Calendar.getInstance();
            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH) + 1;
            int dayNow = cal.get(Calendar.DATE);

            int year = Integer.valueOf(idCardNo.substring(6, 10));
            int month = Integer.valueOf(idCardNo.substring(10, 12));
            int day = Integer.valueOf(idCardNo.substring(12, 14));

            if ((month < monthNow) || (month == monthNow && day <= dayNow)) {
                age = String.valueOf(yearNow - year);
            } else {
                age = String.valueOf(yearNow - year - 1);
            }

        }

        return age;

    }

}
